package com.xebest.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.OrderInfoDTO;
import com.xebest.api.dto.OrderInfoDetailDTO;
import com.xebest.api.dto.SplitOrderInfoDTO;
import com.xebest.api.dto.WishOrderDTO;
import com.xebest.api.service.ForErpOrderIApiService;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.common.enums.erp.order.VenOrderExpFlagEnum;
import com.xebest.common.enums.erp.order.VenOrderMarkEnum;
import com.xebest.common.enums.erp.order.VenOrderSplitStatusEnum;
import com.xebest.common.enums.erp.order.VenOrderStatusEnum;
import com.xebest.common.enums.erp.sys.VenDelEnum;
import com.xebest.common.enums.logtype.ErpLogTypeEnum;
import com.xebest.common.enums.order.OrderSourceEnum;
import com.xebest.common.enums.order.OrderSourceTypeEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.config.SplitOrderSender;
import com.xebest.constant.XeConstans;
import com.xebest.util.XeVenBusConstans;
import com.xebest.util.msg.XeVenOrderMsg;
import com.xebest.web.form.XeVenOrderForm;
import com.xebest.web.form.XeVenSplitOrderDetailForm;
import com.xebest.web.form.XeVenSplitOrderForm;
import com.xebest.web.form.XeVenSplitOrdersForm;
import com.xebest.web.mapper.read.XeVenOrderReadMapper;
import com.xebest.web.mapper.read.XeVenStoreInfoReadMapper;
import com.xebest.web.mapper.write.XeVenOrderWriteMapper;
import com.xebest.web.model.*;
import com.xebest.web.service.XeVenOrderLogisticsService;
import com.xebest.web.service.XeVenOrderService;
import com.xebest.web.service.XeVenSplitOrderService;
import com.xebest.web.service.XeVenStoreInventoryService;
import com.xebest.web.view.ErpUserInfo;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class XeVenSplitOrderServiceImpl implements XeVenSplitOrderService {
	private static final Logger log = LoggerFactory.getLogger(XeVenSplitOrderServiceImpl.class);//日志记录类
	@Autowired
	private XeVenOrderReadMapper xeVenOrderReadMapper;//查询订单业务操作

	@Autowired
	private XeVenOrderWriteMapper xeVenOrderWriteMapper; //更新订单业务操作
	
	@Autowired
	private XeVenStoreInventoryService xeVenStoreInventoryService;//库存一览操作
	
	@Autowired
	private XeVenStoreInfoReadMapper xeVenStoreInfoReadMapper;//查询仓库操作
	
	@Autowired
	private XeVenOrderService xeVenOrderService;//订单操作
	@Autowired
	private XeBaseUtil xeBaseUtil;
	@Autowired
	private XeVenOrderLogisticsService xeVenOrderLogisticsService;
	@Autowired
    private ForErpOrderIApiService forErpOrderIApiService;

	/**
	 * 拆分订单
	 * @author: ruancz  
	 * @createTime: 2017年6月26日 下午7:04:04  
	 * @history:  
	 * @param splitOrdersForm
	 * @param requestIP
     * @return Map<String,Object>
	 * @throws Exception 
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public Map<String, Object> updateSplitOrder(XeVenSplitOrdersForm splitOrdersForm, ErpUserInfo userInfo, String requestIP) throws Exception {
		log.info("订单号: {}开始拆分订单...",splitOrdersForm.getOrderId());
		//返回结果  用来记录是或否有异常订单
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(XeVenBusConstans.CODE,XeVenBusConstans.BUSSINESS_SUCCESS);
		resultMap.put(XeVenBusConstans.MSG,XeVenOrderMsg.SUCCESS);
		
		//原订单id
		String orderId = splitOrdersForm.getOrderId();
		//查询条件封装
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		map.put("userCode", userInfo.getUserCode());
		//拆除异常订单提醒
		StringBuffer errorOrder = new StringBuffer();
		//将原订单置为拆单已删除状态
		XeVenOrderInfoEntity orderInfo = this.xeVenOrderReadMapper.getOrderInfo(map);
        String orderMark = orderInfo.getOrderMark();
        String storeId = orderInfo.getStoreId();
        
        List<XeVenOrderInfoDetailEntity> orderInfoDetailList = orderInfo.getOrderInfoDetailList();
//        Integer orderSource = orderInfo.getOrderSource();
        Integer orderSourceType=orderInfo.getOrderSourceType();
       
        //拆单传给前台DTO
		SplitOrderInfoDTO splitOrderInfoDTO = new SplitOrderInfoDTO();
        splitOrderInfoDTO.setOrderId(orderId);
        //前台订单DTO
        List<OrderInfoDTO> orderInfoListDTO = new ArrayList<OrderInfoDTO>();
		StringBuffer operContent = new StringBuffer();
		operContent.append("拆分订单【"+orderId+"】为");
		//新订单list
		List<XeVenOrderInfoEntity> orderInfoList = new ArrayList<XeVenOrderInfoEntity>();
		//封装订单和订单明细信息
		List<XeVenSplitOrderForm> splitOrderList = splitOrdersForm.getSplitOrderList();
		for (int i = 0; i < splitOrderList.size(); i++) {
			XeVenSplitOrderForm orderForm = splitOrderList.get(i);
			//新订单信息
			XeVenOrderInfoEntity splitOrderInfo = getOrderInfoEntity(orderInfo,orderForm);
			//log.info("新增订单信息为 : {}", JSON.toJSONString(splitOrderInfo));
			orderInfo.setOrderInfoDetailList(null);
			splitOrderInfo.setParentOrderid(orderId);
			splitOrderInfo.setSplitOrderStatus(VenOrderSplitStatusEnum.SPLITED.getValue());
			splitOrderInfo.setUserCode(userInfo.getUserCode());
				
			//封装订单信息DTO
            OrderInfoDTO orderInfoDTO = JSON.parseObject(JSON.toJSONString(splitOrderInfo),OrderInfoDTO.class);
            orderInfoDTO.setOrderStatus(XeConstans._XE_THREE);
            orderInfoDTO.setMorderId(splitOrderInfo.getMainOrderId());
            orderInfoDTO.setModfiyUser(userInfo.getLoginName());
            orderInfoDTO.setModifyDate(new Date());
            orderInfoDTO.setParentOrderid(orderId);
            orderInfoDTO.setPayTime(orderInfo.getPayTime());
            orderInfoDTO.setAutoOperationFlag(orderInfo.getAutoOperationFlag());
            orderInfoDTO.setSplitOrderStatus(VenOrderSplitStatusEnum.NO_SPLITED.getValue());
            orderInfoDTO.setCreateDate(orderInfo.getCreateDate());//v3.1.3订单中心-订单拆单：订单拆单展示原始订单创建的时间
            List<XeVenOrderInfoDetailEntity> splitOrderIndoDetailList =this.toXeVenOrderInfoEntity(orderForm.getSplitOrderDetailList(),splitOrderInfo);
            splitOrderInfo.setOrderInfoDetailList(splitOrderIndoDetailList);
            //封装订单明细DTO
            List<OrderInfoDetailDTO> OrderInfoDetailDTO = JSON.parseArray(JSON.toJSONString(splitOrderIndoDetailList),OrderInfoDetailDTO.class);
            orderInfoDTO.setOrderDetailList(OrderInfoDetailDTO);
            orderInfoListDTO.add(orderInfoDTO);

			//splitOrderInfo.setCreateDate(new Date());
            splitOrderInfo.setCreateDate(orderInfo.getCreateDate());//v3.1.3订单中心-订单拆单：订单拆单展示原始订单创建的时间
			splitOrderInfo.setAutoOperationFlag(orderInfo.getAutoOperationFlag());
			splitOrderInfo.setPayTime(orderInfo.getPayTime());
			splitOrderInfo.setCreaterUser(userInfo.getLoginName());
			splitOrderInfo.setModfiyUser(userInfo.getLoginName());
			splitOrderInfo.setModifyDate(new Date());
			splitOrderInfo.setStoreId(orderForm.getStoreId());
			splitOrderInfo.setOrderStatus(VenOrderStatusEnum.WAIT_CHECK.getValue());
            orderInfoList.add(splitOrderInfo);
			operContent.append("【"+splitOrderInfo.getOrderId()+"】、");
        }
        splitOrderInfoDTO.setOrderInfoList(orderInfoListDTO);

//		log.info("erp拆分订单推送商城平台入参 :{}",JSON.toJSONString(splitOrderInfoDTO));
		BaseResponseDTO baseResponseDTO = forErpOrderIApiService.splitOrder(splitOrderInfoDTO);
		log.info("erp拆分订单推送商城平台返回结果 :{}",JSON.toJSONString(baseResponseDTO));
        if ("2000".equals(baseResponseDTO.getStatus())) {
        	XeVenOrderInfoEntity orderInfoEntit = new XeVenOrderInfoEntity();
			orderInfoEntit.setDelFlag(VenDelEnum.YES.getValue());
			orderInfoEntit.setModfiyUser(userInfo.getLoginName());
			orderInfoEntit.setUserCode(userInfo.getUserCode());
			orderInfoEntit.setParentOrderid("0");
			orderInfoEntit.setOrderId(orderId);
			int infoFlag = this.xeVenOrderWriteMapper.updateXeVenOrderInfo(orderInfoEntit);
			if (infoFlag == XeConstans._XE_ZERO) {
				resultMap.put(XeVenBusConstans.MSG,XeVenOrderMsg.ERROR);
				return resultMap;
			}
			//原订单仓库置为删除状态
			XeVenOrderStoreEntity orderStore = new XeVenOrderStoreEntity();
			orderStore.setModifyUser(userInfo.getLoginName());
			orderStore.setDelFlag(VenDelEnum.YES.getValue());
			orderStore.setStoreId(storeId);
			orderStore.setOrderId(orderId);
			orderStore.setUserCode(userInfo.getUserCode());
			this.xeVenOrderWriteMapper.updateXeVenOrderStore(orderStore);
			//原订单物流信息删除
			XeVenOrderLogistics orderLogistics = new XeVenOrderLogistics();
			orderLogistics.setOrderId(orderId);
			orderLogistics.setUserCode(userInfo.getUserCode());
			orderLogistics.setDelFlag(VenDelEnum.YES.getValue());
			this.xeVenOrderLogisticsService.updateLogisticsByLogtsCode(orderLogistics);
			//原订单占用库存回
			XeVenStoreInfoEntity storeInfo = xeVenStoreInfoReadMapper.getStoreInfoByMap(storeId,userInfo.getUserCode());
			boolean fallbackInventory = this.xeVenOrderService.fallbackInventory(storeInfo, orderInfoDetailList, orderStore.getStoreId(),orderInfo.getUserCode(),userInfo);
			log.info("订单号: {}释放库存结果: {}",splitOrdersForm.getOrderId(),fallbackInventory == true ? "成功" : "失败");
			if (!fallbackInventory) {
				throw new RuntimeException(XeVenOrderMsg.FALLBACK_INVENTORY);
			}
			for (int i = 0; i < orderInfoList.size(); i++) {
				XeVenStoreInfoEntity storeInfoEntity = xeVenStoreInfoReadMapper.getStoreInfoByMap(orderInfoList.get(i).getStoreId(), userInfo.getUserCode());
	//			XeVenSplitOrderForm orderForm = splitOrdersForm.getSplitOrderList().get(i);
				//新订单信息
				XeVenOrderInfoEntity orderInfoEntity = orderInfoList.get(i);
				log.info("拆分新订单号: {} 开始新增订单",orderInfoEntity.getOrderId());
				orderInfoEntity.setParentOrderid(orderId);
				orderInfoEntity.setUserCode(userInfo.getUserCode());
				orderInfoEntity.setOrderStatus(VenOrderStatusEnum.WAIT_CHECK.getValue());
				orderInfoEntity.setCreaterUser(userInfo.getLoginName());
				if (StringUtils.isBlank(orderMark)) {
					orderInfoEntity.setOrderMark(VenOrderMarkEnum.SPLIT.getValue());
				} else {
					orderInfoEntity.setOrderMark(orderMark+","+VenOrderMarkEnum.SPLIT.getValue());
				}
				//新订单明细
	//	  		List<XeVenOrderInfoDetailEntity> splitOrderDetailList =this.toXeVenOrderInfoEntity(orderForm.getSplitOrderDetailList(),orderInfoEntity);
				List<XeVenOrderInfoDetailEntity> splitOrderDetailList = orderInfoEntity.getOrderInfoDetailList();
				//占库存
				int flag = this.xeVenOrderService.judeExceptionOrder(orderInfoEntity, storeInfoEntity, splitOrderDetailList);
				log.info("拆分新订单号: {}在仓库id为: {}占用库存结果为: {}",orderInfoEntity.getOrderId(),storeInfoEntity.getStoreId(), flag == splitOrderDetailList.size() ? "成功" : "失败");
				orderStore.setOrderId(orderInfoEntity.getOrderId());
				orderStore.setStoreId(orderInfoList.get(i).getStoreId());
				orderStore.setCreateDate(new Date());
				orderStore.setCreateUser(userInfo.getLoginName());
				orderStore.setDelFlag(VenDelEnum.NO.getValue());
				orderStore.setUserCode(userInfo.getUserCode());

				//分仓成功 关联订单仓库表
				if (flag != splitOrderDetailList.size()) {
					errorOrder.append("订单号"+orderInfoEntity.getOrderId()+"在"+storeInfoEntity.getStoreName()+"分仓库存不足,置为异常订单");
					//分仓占库存失败 异常订单
					orderInfoEntity.setOrderException(VenDelEnum.YES.getValue());
				}
				//插入订单  订单明细
				orderInfoEntity.setSplitOrderStatus(VenOrderSplitStatusEnum.SPLITED.getValue());
				int info = this.xeVenOrderWriteMapper.insertXeVenOrderInfo(orderInfoEntity);
				if (info == XeConstans._XE_ONE) {
					this.xeVenOrderWriteMapper.insertXeVenOrderStore(orderStore);
					this.xeVenOrderWriteMapper.insertBatchXeVenOrderInfoDetail(splitOrderDetailList);
					//新订单插入物流信息
					this.xeVenOrderService.insertOrderLogistics(userInfo,orderInfoEntity.getOrderId(),storeInfoEntity.getStoreName());
				}
			}
		}
		this.xeVenOrderService.insertMqLog(ErpLogTypeEnum.erpOrderModifySplit.getValue(),orderId,operContent.toString(),userInfo,requestIP,null);
	  	if (errorOrder.length() > XeConstans._XE_ZERO) {
			resultMap.put(XeVenBusConstans.MSG, errorOrder);
		}
	  	//拆单发消息通知给鲜易商城
	  	WishOrderDTO wishOrderDTO = null;
        if(orderSourceType!=null&&orderSourceType.equals(OrderSourceTypeEnum.WISHORDER.getValue())){
        	wishOrderDTO = new WishOrderDTO();
        	wishOrderDTO.setOldRelationOrder(orderId);
        	String nowOrderIds = orderInfoList.stream().map(k->k.getOrderId()).distinct().collect(Collectors.joining(","));
        	wishOrderDTO.setOrderId(nowOrderIds);
        	SplitOrderSender.sender.sendSplitMsg(JSONObject.toJSONString(wishOrderDTO));
        }
		log.info("订单号: {}结束拆分订单...",splitOrdersForm.getOrderId());
		return resultMap;  
	}

	/**
	 * 转换为订单明细
	 * @author: ruancz
	 * @createTime: 2017年6月29日 下午4:04:53
	 * @history:
	 * @param splitOrderDetail
	 * @return XeVenOrderInfoDetailEntity
	 */
	public List<XeVenOrderInfoDetailEntity> toXeVenOrderInfoEntity(List<XeVenSplitOrderDetailForm> splitOrderDetail,XeVenOrderInfoEntity orderInfoEntity) {
		List<XeVenOrderInfoDetailEntity> orderInfoDetailList = new ArrayList<XeVenOrderInfoDetailEntity>();
		for (int i = 0; i < splitOrderDetail.size(); i++) {
			XeVenOrderInfoDetailEntity orderInfoDetail = JSON.parseObject(JSON.toJSONString(splitOrderDetail.get(i)),XeVenOrderInfoDetailEntity.class);
			//插入订单明细
			orderInfoDetail.setItemId(xeBaseUtil.getOrderItemId(orderInfoEntity.getOrderId()));
			orderInfoDetail.setOrderId(orderInfoEntity.getOrderId());
			orderInfoDetail.setProPayAmount(orderInfoDetail.getProTotalPrice());
			orderInfoDetail.setUserCode(orderInfoEntity.getUserCode());
			orderInfoDetail.setCreateUser(orderInfoEntity.getCreaterUser());
			//orderInfoDetail.setCreateDate(new Date());//v3.1.3订单中心-订单拆单：订单拆单展示原始订单创建的时间
			orderInfoDetail.setCreateDate(orderInfoEntity.getCreateDate());
			orderInfoDetailList.add(orderInfoDetail);
		}
		return orderInfoDetailList;
	}
	/**
	 * 取消拆单
	 * @author: ruancz  
	 * @createTime: 2017年6月27日 上午9:53:13  
	 * @history:  
	 * @param orderForm
	 * @param requestIP
	 * @return Map<String, Object>
	 * @throws Exception 
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public Map<String, Object> updateUnSplitOrder(XeVenOrderForm orderForm, ErpUserInfo userInfo, String requestIP) throws Exception {
		log.info("订单号: {}开始取消拆分订单...",orderForm.getOrderId());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(XeVenBusConstans.CODE,XeVenBusConstans.BUSSINESS_SUCCESS);
		resultMap.put(XeVenBusConstans.MSG,XeVenOrderMsg.SUCCESS);

		//当前 待发货”/“已发货”/“已完成”/“已取消 则不允许取消拆分。
		boolean flag = judgeCancel(orderForm.getOrderStatus());
		if (flag) {
			resultMap.put(XeVenBusConstans.CODE,XeVenBusConstans.BUSSINESS_ERROR);
			resultMap.put(XeVenBusConstans.MSG,"当前订单"+orderForm.getOrderStatus()+"已审核/已发货/已完成/已取消，无法对其进行取消拆分");
			return resultMap;
		}
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode",userInfo.getUserCode());
		map.put("parentOrderId",orderForm.getParentOrderid());
		List<XeVenOrderInfoEntity> list = this.xeVenOrderReadMapper.queryOrderInfoByParams(map);
		map.remove("parentOrderId");
		//用于更新意向单的关联订单
		WishOrderDTO wishOrderDto = null;
		Integer orderSource = list.get(0).getOrderSource();
		
		//判断是否包含不允许取消拆分标识
		boolean judgeFlag = true;
		//记录不允许取消拆分订单id
		String judgeOrderId = null;
		//同级 待发货”/“已发货”/“已完成”/“已取消 则不允许取消拆分。
		StringBuffer operContent = new StringBuffer();
		operContent.append("取消订单【"+orderForm.getOrderId()+"】的拆分，同时取消子订单");
		for (int i = 0; i < list.size(); i++) {
			operContent.append("【"+list.get(i).getOrderId()+"】,");
			if (judgeCancel(list.get(i).getOrderStatus())) {
				log.info("订单号: {}是已取消/已完成/待发货/已审核订单，不允许取消拆分",list.get(i).getOrderId());
				judgeOrderId = list.get(i).getOrderId();
				judgeFlag = false;
				break;
			}
		}
		operContent.append("并还原为主订单【"+orderForm.getParentOrderid()+"】。");
		if (judgeFlag) {
		    //前台取消拆分
			SplitOrderInfoDTO splitOrderInfoDTO = new SplitOrderInfoDTO();
			splitOrderInfoDTO.setOrderId(orderForm.getParentOrderid());
            BaseResponseDTO baseResponseDTO = this.forErpOrderIApiService.offSplitOrder(splitOrderInfoDTO);
			log.info("取消拆单返回结果 :{}",JSON.toJSONString(baseResponseDTO));
            if (!"2000".equals(baseResponseDTO.getStatus())) {
                resultMap.put(XeVenBusConstans.CODE,XeVenBusConstans.BUSSINESS_ERROR);
                resultMap.put(XeVenBusConstans.MSG,XeVenOrderMsg.ERROR);
                return resultMap;
            }

            //拆分后所有订单 库存回退 订单置为删除
			for (int i = 0; i < list.size(); i++) {
				XeVenOrderInfoEntity orderInfo = list.get(i);
				orderInfo.setOrderStatus(VenOrderStatusEnum.CANCEL.getValue());
				orderInfo.setDelFlag(VenDelEnum.YES.getValue());
				orderInfo.setModfiyUser(userInfo.getLoginName());
				orderInfo.setUserCode(userInfo.getUserCode());
				int infoFlag = this.xeVenOrderWriteMapper.updateXeVenOrderInfo(orderInfo);
				if (infoFlag == XeConstans._XE_ZERO) {
					resultMap.put(XeVenBusConstans.MSG,XeVenOrderMsg.ERROR);
					return resultMap;
				}
				//子单仓库置为已删除
				XeVenOrderStoreEntity orderStore = new XeVenOrderStoreEntity();
				orderStore.setOrderId(orderInfo.getOrderId());
				orderStore.setModifyUser(userInfo.getLoginName());
				orderStore.setDelFlag(VenDelEnum.YES.getValue());
				orderStore.setUserCode(userInfo.getUserCode());
				this.xeVenOrderWriteMapper.updateXeVenOrderStore(orderStore);
				//删除物流单信息
				XeVenOrderLogistics orderLogistics = new XeVenOrderLogistics();
				orderLogistics.setOrderId(orderInfo.getOrderId());
				orderLogistics.setDelFlag(VenDelEnum.YES.getValue());
				orderLogistics.setUserCode(userInfo.getUserCode());
				this.xeVenOrderLogisticsService.updateLogisticsByLogtsCode(orderLogistics);

				XeVenStoreInfoEntity storeInfo = this.xeVenStoreInfoReadMapper.getStoreInfoByMap(orderInfo.getStoreId(), userInfo.getUserCode());
				map.put("orderId", orderInfo.getOrderId());
				List<XeVenOrderInfoDetailEntity> infoDetailList = this.xeVenOrderReadMapper.getOrderDetailByParam(map);
				boolean fallbackInventory = this.xeVenOrderService.fallbackInventory(storeInfo, infoDetailList, orderInfo.getStoreId(), orderInfo.getUserCode(),userInfo);
				log.info("拆分订单号: {} 释放库存结果: {}",orderInfo.getOrderId(),fallbackInventory == true ? "成功" : "失败");
				if (!fallbackInventory) {
					throw new RuntimeException(XeVenOrderMsg.FALLBACK_INVENTORY);
				}
			}
			map.put("orderId",orderForm.getParentOrderid());
			//原订单回复原有状态
			XeVenOrderInfoEntity orderInfoEntity = this.xeVenOrderReadMapper.getOrderInfo(map);
			log.info("取消拆单查询数据为 : {}", JSON.toJSONString(orderInfoEntity));
			orderInfoEntity.setSplitOrderStatus(VenOrderSplitStatusEnum.NO_SPLITED.getValue());
			orderInfoEntity.setDelFlag(VenDelEnum.NO.getValue());
			orderInfoEntity.setModfiyUser(userInfo.getLoginName());
			orderInfoEntity.setUserCode(userInfo.getUserCode());
			
			//查询订单仓库信息 明细信息
			XeVenStoreInfoEntity storeInfo = this.xeVenStoreInfoReadMapper.getStoreInfoByMap(orderInfoEntity.getStoreId(), userInfo.getUserCode());
			List<XeVenOrderInfoDetailEntity> infoDetailList = orderInfoEntity.getOrderInfoDetailList();
			
			//原订单 重新占库存
			int result = this.xeVenOrderService.judeExceptionOrder(orderInfoEntity, storeInfo, infoDetailList);
			XeVenOrderStoreEntity orderStore = new XeVenOrderStoreEntity();
			orderStore.setOrderId(orderInfoEntity.getOrderId());
			orderStore.setDelFlag(VenDelEnum.NO.getValue());
			orderStore.setModifyUser(userInfo.getLoginName());
			orderStore.setUserCode(userInfo.getUserCode());
			//物流信息回复
			XeVenOrderLogistics orderLogistics = new XeVenOrderLogistics();
			orderLogistics.setOrderId(orderInfoEntity.getOrderId());
			orderLogistics.setUserCode(userInfo.getUserCode());
			orderLogistics.setDelFlag(VenDelEnum.YES.getValue());
			this.xeVenOrderLogisticsService.updateLogisticsByLogtsCode(orderLogistics);
			//全部占库存
			if (result == infoDetailList.size()) {
				orderInfoEntity.setOrderException(VenOrderExpFlagEnum.NO_PROBLEM.getValue());
			} else {
				//部分占库存
				orderInfoEntity.setOrderException(VenOrderExpFlagEnum.PROBLEM.getValue());
			}
			int infoFlag = this.xeVenOrderWriteMapper.updateXeVenOrderInfo(orderInfoEntity);
			if (infoFlag > XeConstans._XE_ZERO) {
				this.xeVenOrderWriteMapper.updateXeVenOrderStore(orderStore);
			}
			this.xeVenOrderService.insertMqLog(ErpLogTypeEnum.erpOrderModifyUnSplit.getValue(),orderForm.getParentOrderid(),operContent.toString(),userInfo,requestIP,null);
			//订单拆单消息通知的方式更新意向单
			if(orderSource == OrderSourceEnum.WISHORDER.getValue()){
				 wishOrderDto = new WishOrderDTO();
				 wishOrderDto.setOldRelationOrder(list.stream().map(k -> k.getOrderId()).distinct().collect(Collectors.joining(",")));
				 wishOrderDto.setOrderId(orderForm.getParentOrderid());
				 SplitOrderSender.sender.sendSplitMsg(JSONObject.toJSONString(wishOrderDto));
			}
		} else {
			resultMap.put(XeVenBusConstans.MSG,"当前订单关联的子订单"+judgeOrderId+"已审核/已发货/已完成/已取消，无法对其进行取消拆分");
		}
		
		log.info("订单号: {}结束取消拆分订单...",orderForm.getOrderId());
		return resultMap;  
	}

	private boolean judgeCancel(Integer flag) {
		return VenOrderStatusEnum.WAIT_SEND.getValue() == flag ||
						VenOrderStatusEnum.CANCEL.getValue() == flag ||
						VenOrderStatusEnum.FINISH.getValue() == flag ||
						VenOrderStatusEnum.SEND.getValue() == flag;
	}

	/**
	 * 新增订单拼装信息
	 * @author: ruancz  
	 * @createTime: 2017年6月27日 上午9:53:13  
	 * @history:  
	 * @param orderInfo
	 * @param ordersForm
	 * @return XeVenOrderInfoEntity
	 */
	private XeVenOrderInfoEntity getOrderInfoEntity(XeVenOrderInfoEntity orderInfo,XeVenSplitOrderForm orderForm) {
		XeVenOrderInfoEntity orderInfoEntity = JSON.parseObject(JSON.toJSONString(orderInfo),XeVenOrderInfoEntity.class);
		orderInfoEntity.setOrderId(xeBaseUtil.getOrderInfoId());
		log.info("新订单号为 : {}",orderInfoEntity.getOrderId());
		orderInfoEntity.setTotalAmount(orderForm.getTotalAmount());
		/*BigDecimal payAmount = orderForm.getTotalAmount()
			.add(orderForm.getFreAmount() == null ? new BigDecimal("0") : orderForm.getFreAmount())
			.subtract(orderForm.getPmtAmount() == null ? new BigDecimal("0") : orderForm.getPmtAmount());*/
		orderInfoEntity.setPayAmount(orderForm.getPayAmount());
		orderInfoEntity.setGoodsAmount(orderForm.getGoodsAmount());
		orderInfoEntity.setFreAmount(orderForm.getFreAmount());
		orderInfoEntity.setPmtAmount(orderForm.getPmtAmount());
		orderInfoEntity.setCpnsPmtAomount(orderForm.getCpnsPmtAomount());
		orderInfoEntity.setPlatPmtAmount(orderForm.getPlatPmtAmount());
		orderInfoEntity.setSpecialDisamount(orderForm.getSpecialDisamount());
		orderInfoEntity.setAutoMatic(XeConstans._XE_TWO);
		return orderInfoEntity;
	}
	
	public static void main(String[] args) {
		List<String> cities = Arrays.asList("Milan", 
                "London", 
                "New York", 
                "San Francisco");
		String citiesCommaSeparated = String.join(",", cities);
		System.out.println(citiesCommaSeparated);
	}
}
